﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using QLNhaThuoc.DTO;
using QLNhaThuoc.MyClass;

namespace QLNhaThuoc.DAL
{
    class NhanVienDA:DataProvider
    {
        private NhanVienFactory nvFactory;
        public NhanVienDA()
        {
            connect();
            fillMyTable("NHAN_VIEN");
            nvFactory = new NhanVienFactory();
        }

        public void insert(NhanVien info)
        {
            DataRow row = myTable.NewRow();
            row["MaNV"] = info.MaNV;
            row["Ho"] = info.Ho;
            row["Ten"] = info.Ten;
            row["LoaiNV"] = info.LoaiNV;
            row["GioiTinh"] = info.GioiTinh;
            row["NgaySinh"] = info.NgaySinh;
            row["DiaChi"] = info.DiaChi;
            row["SDT"] = info.SDT;
            row["NgayVaoLam"] = info.NgayVaoLam;
            row["Username"] = info.Username;
            row["Password"] = info.Password;
            myTable.Rows.Add(row);

            updateMyTable();
        }

        public void delete(NhanVien info)
        {
            myTable.Rows.Find(info.MaNV).Delete();

            updateMyTable();
        }

        public void update(NhanVien info)
        {
            DataRow row = myTable.Rows.Find(info.MaNV);
            row["MaNV"] = info.MaNV;
            row["Ho"] = info.Ho;
            row["Ten"] = info.Ten;
            row["LoaiNV"] = info.LoaiNV;
            row["GioiTinh"] = info.GioiTinh;
            row["NgaySinh"] = info.NgaySinh;
            row["DiaChi"] = info.DiaChi;
            row["SDT"] = info.SDT;
            row["NgayVaoLam"] = info.NgayVaoLam;
            row["Username"] = info.Username;
            row["Password"] = info.Password;

            updateMyTable();
        }

        public bool KiemTraPK(NhanVien info)
        {
            DataRow row = myTable.Rows.Find(info.MaNV);
            if (row != null)
                return true;
            return false;
        }

        public NhanVien LayNhanVienTheoMa(string MaNV)
        {
            string sqlString = "select * from NHAN_VIEN where MaNV='" + MaNV + "'";
            ArrayList list = DongHopDuLieuTuCustomDatatable(sqlString);

            if (list.Count == 0)
                return null;

            NhanVien nv = nvFactory.TaoNhanVien((NhanVien)list[0]);
            return nv;
        }

        public NhanVien LayNhanVienTheoUserName(string username)
        {
            string sqlString = "select * from NHAN_VIEN where Username = '" + username + "'";
            ArrayList list = DongHopDuLieuTuCustomDatatable(sqlString);

            if (list.Count == 0)
                return null;
            
            NhanVien nv = nvFactory.TaoNhanVien((NhanVien)list[0]);
            return nv;
        }

        public NhanVien[] LayListNhanVien()
        {
            ArrayList list = DongHopDuLieuTuDataTable();
            int n = list.Count;
            NhanVien[] kq = new NhanVien[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (NhanVien)list[i];
            }

            return kq;
        }

        public override object ConvertOneRowIntoDTO(DataTable table, int i)
        {
            string loaiNV = table.Rows[i]["LoaiNV"].ToString();
            NhanVien obj = nvFactory.TaoNhanVien(loaiNV);
            obj.MaNV = table.Rows[i]["MaNV"].ToString();
            obj.Ho = table.Rows[i]["Ho"].ToString();
            obj.Ten = table.Rows[i]["Ten"].ToString();
            obj.LoaiNV = loaiNV;
            obj.GioiTinh = bool.Parse(table.Rows[i]["GioiTinh"].ToString());
            obj.NgaySinh = DateTime.Parse(table.Rows[i]["NgaySinh"].ToString());
            obj.DiaChi = table.Rows[i]["DiaChi"].ToString();
            obj.SDT = table.Rows[i]["SDT"].ToString();
            obj.NgayVaoLam = DateTime.Parse(table.Rows[i]["NgayVaoLam"].ToString());
            obj.Username = table.Rows[i]["Username"].ToString();
            obj.Password = table.Rows[i]["Password"].ToString();

            return obj;
        }
    }
}
